package com.per.mybootall.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
    public static void main(String[] args) throws IOException {

        try{
        //建立套接字
        ServerSocket server=new ServerSocket(4700);
        //监听
        Socket socket=server.accept();
            System.out.println("有一个链接进来了==》》》》");
        //建立链接
        InputStreamReader Sysin=new InputStreamReader(System.in);
        BufferedReader SysBuf=new BufferedReader(Sysin);

        //建立连接【从socket读取】
        InputStreamReader Socin=new InputStreamReader(socket.getInputStream());
        BufferedReader SocBuf=new BufferedReader(Socin);

        //向socket中写
        PrintWriter SocOut=new PrintWriter(socket.getOutputStream());

        System.out.println("server接收的Client:"+SocBuf.readLine());
        String readLine=  SysBuf.readLine();
        while(!readLine.equals("over")){
            //发送数据到客户端
            SocOut.println(readLine);
            SocOut.flush();
            System.out.println("server即将发送的消息为:"+readLine);
            if(!SocBuf.readLine().equals("over the connect")){
                System.out.println("clien发送的消息为："+SocBuf.readLine());
            }else{
                System.exit(1);
            }
            //从键盘读取数据再次发送到客户端
            readLine=SysBuf.readLine();
        }
        //关闭socker和io
        socket.close();
        Socin.close();
        SocOut.close();
        } catch (Exception e) {
            System.out.println("error:"+e);
        }
    }
}
